from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html

from app.routes.ClasRoute import clas_router
from app.routes.StudentRoute import student_route

app = FastAPI(docs_url=None)   # app初始化
app.include_router(clas_router, prefix="/api")
app.include_router(student_route, prefix="/api")

# 挂载静态文件路径
app.mount("/asserts", StaticFiles(directory="app/asserts"), name="asserts")


@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title="Custom Swagger UI",
        swagger_js_url="/asserts/swagger-ui-bundle.js",
        swagger_css_url="/asserts/swagger-ui.css"
    )
# 添加 CORS 中间件，允许所有来源
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源
    allow_credentials=True,  # 允许携带凭证（如 cookies）
    allow_methods=["*"],  # 允许所有 HTTP 方法（GET, POST, PUT 等）
    allow_headers=["*"],  # 允许所有请求头
)


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8888)